﻿using System;
using System.Linq;
using Service.Core.Interface;
using Service.Core.Result;

namespace Service.Core
{
    /// <summary>
    ///     服务层容器
    /// </summary>
    public class BaseService : IService
    {
        /// <summary>
        ///     处理结果
        /// </summary>
        protected IServiceResult _result;

        #region 处理结果

        public IServiceResult Success()
        {
            return Result(ResultType.Success, null, null);
        }

        public IServiceResult Success(string msg)
        {
            return Result(ResultType.Success, null, msg);
        }

        public IServiceResult Success(object reData)
        {
            return Result(ResultType.Success, reData, null);
        }

        public IServiceResult Success(object reData, string msg)
        {
            return Result(ResultType.Success, reData, msg);
        }

        /// <summary>
        ///     处理返回状态
        /// </summary>
        /// <param name="resultType">处理结果类型</param>
        /// <returns>通用的返回结果对象</returns>
        public IServiceResult Result(ResultType resultType)
        {
            return Result(resultType, null, null);
        }

        /// <summary>
        ///     处理返回状态,返回提示
        /// </summary>
        /// <param name="resultType">处理结果类型</param>
        /// <param name="resultMsg">提示信息</param>
        /// <returns>通用的返回结果对象</returns>
        public IServiceResult Result(ResultType resultType, string resultMsg)
        {
            return Result(resultType, null, resultMsg);
        }

        /// <summary>
        ///     处理返回状态,返回提示
        /// </summary>
        /// <param name="resultType">处理结果类型</param>
        /// <param name="reData">返回数据</param>
        /// <returns>通用的返回结果对象</returns>
        public IServiceResult Result(ResultType resultType, object reData)
        {
            return Result(resultType, reData, null);
        }

        /// <summary>
        ///     友好性封装，作用于Controller,Filter的Service结果调用
        /// </summary>
        /// <param name="resultType">处理结果类型</param>
        /// <param name="reData">返回数据</param>
        /// <param name="resultMsg">提示信息</param>
        /// <returns>通用的返回结果对象</returns>
        public IServiceResult Result(ResultType resultType, object reData, string resultMsg)
        {
            _result = new NativeResult();
            _result.SetCode(resultType);
            _result.SetData(reData);
            _result.SetMsg(resultMsg);

            return _result;
        }

        #endregion

        #region 参数

        /// <summary>
        ///     参数为空
        /// </summary>
        /// <param name="inParams">实体参数</param>
        /// <returns>true:为空 false:不为空 </returns>
        public static bool ParamsIsNull(params object[] inParams)
        {
            if (inParams.Any(ParamIsNullOrEmpty))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     参数为空或者空初始
        /// </summary>
        /// <param name="inParam"></param>
        /// <returns>T:为空 F:不为空</returns>
        private static bool ParamIsNullOrEmpty(object inParam)
        {
            if (inParam == null)
            {
                return true;
            }

            if (ReferenceEquals(inParam.GetType(), Type.GetType("System.Guid")))
            {
                return (Guid)inParam == Guid.Empty;
            }

            if (ReferenceEquals(inParam.GetType(), Type.GetType("System.String")))
            {
                return String.IsNullOrEmpty((string)inParam);
            }

            return false;
        }

        /// <summary>
        ///     匹配最小最大
        /// </summary>
        /// <param name="input">验证字符</param>
        /// <param name="min">最小字符</param>
        /// <param name="max">最大字符</param>
        /// <returns>True:匹配 False：不匹配</returns>
        public bool StringLength(string input, int min, int max)
        {
            return input.Length >= min && input.Length <= max;
        }

        #endregion

        #region Pager


        #endregion
    }
}